﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CrudCategories.Web;
using System.Windows.Data;

namespace CrudCategories
{
    public partial class MainPage : UserControl
    {
        private DomainServiceCategory dsCategory;

        public MainPage()
        {
            InitializeComponent();
            this.dsCategory = new DomainServiceCategory();
            PagedCollectionView categoriesView =
                new PagedCollectionView(dsCategory.Categories);
            dsCategory.Load(dsCategory.GetCategoriesQuery());
            dataGridCategories.ItemsSource = categoriesView;
            dataPagerCategories.Source = categoriesView;
        }

        private void buttonAddCategory_Click(object sender, RoutedEventArgs e)
        {
            AddCategory addCategory = new AddCategory();
            addCategory.Closed += addCategory_Closed;
            addCategory.Show();
        }

        void addCategory_Closed(object sender, EventArgs e)
        {
            AddCategory addCategory = (AddCategory)sender;
            if (addCategory.DialogResult == true)
            {
                //dataGridCategories.ItemsSource = null;
                //this.dsCategory = new DomainServiceCategory();
                //this.dsCategory.Load(this.dsCategory.GetCategoriesQuery());
                //dataGridCategories.ItemsSource = this.dsCategory.Categories;
                this.dsCategory.Categories.Add(addCategory.NewCategory);
                this.dsCategory.SubmitChanges();
                this.dsCategory.Load(this.dsCategory.GetCategoriesQuery());
            }
        }

        private void buttonEdit_Click(object sender, RoutedEventArgs e)
        {
            Category categoryToEdit = (Category)dataGridCategories.SelectedItem;
            if (categoryToEdit != null)
            {
                EditCategory editCategory = new EditCategory();
                editCategory.CategoryToEdit = categoryToEdit;
                editCategory.Closed += editCategory_Closed;
                editCategory.Show();
            }
            else
            {
                MessageBox.Show("Select category to edit");
            }
        }

        void editCategory_Closed(object sender, EventArgs e)
        {
            EditCategory editCategory = (EditCategory)sender;
            if (editCategory.DialogResult == true)
            {
                this.dsCategory.SubmitChanges();
                this.dsCategory.Load(this.dsCategory.GetCategoriesQuery());
            }
            else
            {
                this.dsCategory.RejectChanges();
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            Category category = (Category)dataGridCategories.SelectedItem;
            if (category != null)
            {
                this.dsCategory.Categories.Remove(category);
                this.dsCategory.SubmitChanges();
            }
            else
            {
                MessageBox.Show("Select category to delete");
            }
        }        
    }
}
